package com.sunflower.asone.system.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.sunflower.framework.application.BaseController;
import org.sunflower.framework.utils.JsonResult;

@Controller
@RequestMapping(value = "index")
public class LoginController extends BaseController{
	
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@RequestMapping(value = "", method = { RequestMethod.GET })
	public String index(ModelMap map, HttpServletRequest request,HttpServletResponse response) {
		/*Map<String, String> rmap = new HashMap<String, String>();
		rmap.put("name", "fujianchao");
		rmap.put("password", "123");
		rmap.put("age", "12");
		RedisPools jedisPool = new RedisPools();
		jedisPool.setJedisMap(RedisUtils.SERVICE_REDIS_COMMON, "myMap", rmap);

		Map<String, String> newMap = new HashMap<String, String>();

		newMap = jedisPool.getJedisMap(RedisUtils.SERVICE_REDIS_COMMON, "myMap");

		System.out.println(newMap.size());*/
		
		return "user/index";
	}
	
	@RequestMapping(value = "/1", method = { RequestMethod.GET })
	public String ninestars(ModelMap map, HttpServletRequest request,HttpServletResponse response) {
		return "user/Ninestars";
	}
	
	
	
	@ResponseBody
	@RequestMapping(value = "/userLogin", method = { RequestMethod.POST })
	public JsonResult userLogin(ModelMap map, HttpServletRequest request,HttpServletResponse response) {
		JsonResult jsonResult = JsonResult.newInstance();
		String userName = request.getParameter("username");
		String passWord = request.getParameter("password");
		//鐧诲綍澶辫触
		if(!userName.equals("root") || !passWord.equals("root")){
			jsonResult.declareFailure("login error!");
			jsonResult.addContent("redirect", "/system/logout");
			return jsonResult;
		}
		//鐧诲綍鎴愬姛
		jsonResult.declareSuccess();
	    jsonResult.addContent("redirect", "/system/main");
		return jsonResult;
	}
	
	@RequestMapping(value = "/logout", method = { RequestMethod.GET })
	public String logout() {
		return "system/login";
	}
	
	@RequestMapping(value = "/main", method = { RequestMethod.GET })
	public String loginMain(ModelMap map) {
		return "model/model";
	}
}
